package com.hzdl.library.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzdl.library.domain.Admin;
import com.hzdl.library.domain.PageInformation;
import com.hzdl.library.domain.ReaderCard;
import com.hzdl.library.domain.ReaderInfo;
import com.hzdl.library.service.AdminService;
import com.hzdl.library.service.SelectUserByOne;
import com.hzdl.library.service.impl.AdminServiceImpl;
import com.hzdl.library.service.impl.SelectUserByOneImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//唐得鑫
//管理员
@WebServlet("/admin/*")
public class AdminServlet extends BaseServlet {
    ObjectMapper objectMapper = new ObjectMapper();
//    登录
    public void login(HttpServletRequest request, HttpServletResponse response){
        Integer id = Integer.parseInt(request.getParameter("id"));
        String passwd = request.getParameter("passwd");
        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.selectAdmin(id,passwd);
        PageInformation information = new PageInformation();
        HttpSession session = request.getSession();
//        查询信息码、0是未查到、1是管理员、2是用户
        int code = 0;
        if(admin != null) {
            information.setObj(admin);
            code = 1;
            session.setAttribute("admin_id",admin.getAdmin_id());
        }else{
//            调用查询用户的方法
            SelectUserByOne selectUserByOne = new SelectUserByOneImpl();
            ReaderCard readerCard = selectUserByOne.selectReaderCard(id,passwd);
            if (readerCard != null) {
                ReaderInfo readerInfo = selectUserByOne.selectReader(id);
                information.setObj(readerInfo);
                code = 2;
                session.setAttribute("readerInfo",readerInfo);
            }
        }
        information.setCode(code);
//        System.out.println(information);

        writeValue(response,information);
    }
//    修改密码
    public void modifyPass(HttpServletRequest request, HttpServletResponse response){
        Integer id = Integer.parseInt(request.getParameter("id"));
        String oldPass = request.getParameter("oldPass");
        String newPass = request.getParameter("newPass");
        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.selectAdmin(id,oldPass);
//        System.out.println(admin);
        PageInformation information = new PageInformation();
        if(admin == null){
            information.setCode(0);
            information.setMessage("密码错误");
            writeValue(response,information);
            return;
        }
        if (adminService.updateAdmin(id,newPass) < 1){
            information.setCode(0);
            information.setMessage("修改失败");
            writeValue(response,information);
            return;
        }
        information.setCode(1);
        information.setMessage("修改成功");
        writeValue(response,information);
    }
    //获取管理员id
    public void getAdmin(HttpServletRequest request, HttpServletResponse response){
        Integer admin_id = (Integer) request.getSession().getAttribute("admin_id");
//        System.out.println(admin_id);
        PageInformation information = new PageInformation();
        information.setObj(admin_id);
        writeValue(response,information);
        //
    }
    //清除管理员id
    public void quitAdmin(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.removeAttribute("admin_id");
        PageInformation information = new PageInformation();
        writeValue(response,information);
    }
//    响应页面
    private void writeValue(HttpServletResponse response,PageInformation information){
        try {
            objectMapper.writeValue(response.getOutputStream(),information);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
